package com.zw.controller.user;

import com.zw.core.domain.AjaxResult;
import com.zw.dto.BudgetDto;
import com.zw.dto.BudgetSortDto;
import com.zw.dto.ImportBudgetDto;
import com.zw.entity.Budget;
import com.zw.entity.BudgetColumnsContent;
import com.zw.service.BudgetService;
import com.zw.vo.BudgetVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预算
 */
@Tag(name = "预算")
@RestController("userBudgetController")
@RequestMapping("/user/budget")
public class BudgetController {

    @Resource
    private BudgetService budgetService;

    @Operation(summary = "修改预算项")
    @PutMapping
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult updateBudget(@RequestBody Budget budget) {
        budgetService.updateBudget(budget);
        return AjaxResult.success();
    }

    @Operation(summary = "删除预算项")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult deleteBudget(@PathVariable Long id) {
        budgetService.removeBudget(id);
        return AjaxResult.success();
    }

    @Operation(summary = "新增预算项")
    @PostMapping
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult addBudget(@RequestBody Budget budget) {
        return AjaxResult.success(budgetService.saveBudget(budget));
    }

    @Operation(summary = "更新预算项排序")
    @PreAuthorize("@ss.authenticated()")
    @PutMapping("/updateSort")
    public AjaxResult updateBudgetSort(@RequestBody @Valid List<BudgetSortDto> sortRequests) {
        budgetService.updateBudgetSort(sortRequests);
        return AjaxResult.success();
    }

    @Operation(summary = "查询预算项")
    @PostMapping("/queryBudget")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult queryBudget(@RequestBody @Valid BudgetDto budgetDto) {
        //获取对应的预算项并整合成vo反馈给前端 当parentId = 0 是查的是第一层预算项的数据.
        List<BudgetVo> res = budgetService.getBudgetDetail(budgetDto);
        return AjaxResult.success(res);
    }

    @Operation(summary = "保存或更新预算项")
    @PostMapping("/saveOrUpdateBudgets")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult saveOrUpdateBudgets(@Valid @RequestBody List<BudgetColumnsContent> budgetColumns) {
        budgetService.saveOrUpdateBudgets(budgetColumns);
        return AjaxResult.success();
    }


    @Operation(summary = "导入预算项")
    @PostMapping("/import")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult importBudget(@Valid @RequestBody List<ImportBudgetDto> importBudgetDtos) {
        budgetService.importBudget(importBudgetDtos);
        return AjaxResult.success();
    }

}
